<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>富文本编辑器</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">

    <style type="text/css">
        .toolbar {
            border: 1px solid #ccc;
        }
        .text {
            border: 1px solid #ccc;
            height: 520px;
        }

        .layui-form-select {
            z-index: 9999999;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <div class="">
            <input type="text" name="articleName" lay-verify="required" lay-reqtext="文章标题不能为空" placeholder="请输入文章标题" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-inline">
            <select name="modules" lay-verify="required" lay-search=""  id="categoryName" name="categoryId" lay-filter="categoryId">
                <option value="">选择类别</option>
            </select>
        </div>
    </div>

    <div class="layuimini-container layui-form-item">
        <div class="layuimini-main">
            <div id="div1" class="toolbar">
            </div>
            <div id="div2" class="text min-height" > <!--可使用 min-height 实现编辑区域自动增加高度-->
            </div>
        </div>
    </div>

    <div class="layui-form-item " style="text-align: center;">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">保存为草稿</button>
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="publishBtn">保存并发布</button>
    </div>
</div>



<!-- 注意， 只需要引用 JS，无需引用任何 CSS ！！！-->
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>

<script type="text/javascript">
    layui.use(['layer','wangEditor','form'], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            form = layui.form,
            wangEditor = layui.wangEditor;

        var editor = new wangEditor('#div1', '#div2');

        editor.customConfig.uploadImgServer = "/common/uploadImage";
        editor.customConfig.uploadFileName = 'file';
        editor.customConfig.pasteFilterStyle = false;
        editor.customConfig.uploadImgMaxLength = 5;
        editor.customConfig.uploadImgHooks = {
            // 上传超时
            timeout: function (xhr, editor) {
                layer.msg('上传超时！')
            },
            customInsert: function (insertImg, result, editor) {
                // 图片上传并返回结果，自定义插入图片的事件（而不是编辑器自动插入图片！！！）
                // insertImg 是插入图片的函数，editor 是编辑器对象，result 是服务器端返回的结果

                // 举例：假如上传图片成功后，服务器端返回的是 {url:'....'} 这种格式，即可这样插入图片：
                var url = result.url;
                insertImg(url)

                // result 必须是一个 JSON 格式字符串！！！否则报错
            }
        };
        editor.create();

        $.ajax({
            url: '/category/select',//json文件位置
            type: 'get', //请求方式为get
            dataType: 'json', //返回数据格式为json
            success: function (result) { //请求成功完成后要执行的方法
                var select = $("#categoryName");//TypeID为HTML标签ID
                $.each(result.data, function (index, item) {
                    select.append(new Option( item.categoryName, item.id));// 下拉菜单里添加元素
                });
                layui.form.render("select");
            }
        });

        //获取选择框的值
        var categoryId='';
        form.on('select(categoryId)', function (data) {
            categoryId=data.value;
        });
        //保存为草稿
        form.on('submit(saveBtn)', function (data) {
            data = data.field;
            saveOrPublish(0,data.articleName,categoryId,editor.txt.html());
            return false;
        });
        //保存并发布
        form.on('submit(publishBtn)', function (data) {
            data = data.field;
            saveOrPublish(1,data.articleName,categoryId,editor.txt.html());
            return false;
        });
    });

    //保存草稿或者发布
    function saveOrPublish(status,articleName,categoryId,text) {
        $.ajax({
            type:"post",
            url:"/article/add",
            data:{
                "articleName":articleName,
                "categoryId":categoryId,
                "content":text,
                "articleStatus":status
            },
            dataType: 'json',
            success:function (result) {
                if(result.code==web_status.SUCCESS){
                    // 关闭弹出层
                    var iframeIndex = parent.layer.getFrameIndex(window.name);
                    parent.layui.table.reload('currentTableId');
                    parent.layer.close(iframeIndex);
                }else{
                    layer.msg(result.msg);
                }
            }
        });
    }
</script>
<script type="text/javascript" src="/js/ershui/common.js"></script>
<script type="text/javascript" src="/js/jquery-1.7.2.min.js"></script>
</body>
</html>